//
//  ResourceItemModel.m
//  YJC
//
//  Created by IOS3 on 2019/1/14.
//  Copyright © 2019 ZJP. All rights reserved.
//

#import "ResourceItemModel.h"

@implementation ResourceItemModel



- (nonnull NSURL *)so_downloadURL {
    return [NSURL URLWithString:self.downloadPath];
}


- (NSString *)savePath {
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"document path: %@", docPath);
    
    NSString *saveDir = [docPath stringByAppendingPathComponent:@"local"];
    
    BOOL hasDir = [[NSFileManager defaultManager] fileExistsAtPath:saveDir];
    if (!hasDir) {
        [[NSFileManager defaultManager] createDirectoryAtPath:saveDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *fileName = [self.downloadPath componentsSeparatedByString:@"/"].lastObject;
    NSString *savePath = [saveDir stringByAppendingPathComponent:fileName];
    return savePath;
}


/// 资源类型
- (ResourceItemType)getResouceItemType {
    ResourceItemType type = ResourceItemTypeNone;
    
    if ([self.ext isEqualToString:@"视频资源"]) {
        type = ResourceItemTypeVideo;
    }
    
    else if ([self.ext isEqualToString:@"图片资源"]) {
        type = ResourceItemTypePic;
    }
    
    else if ([self.ext isEqualToString:@"图文资源"]) {
        type = ResourceItemTypeDemo;
    }
    
    else if ([self.ext isEqualToString:@"-1"]) {
        
    }
    
    else if ([self.ext isEqualToString:@"-1"]) {
        
    }
    
    return type;
}


#pragma mark - 判等
- (BOOL)isEqual:(id)object {
    if (![object isKindOfClass:[ResourceItemModel class]]) {
        return [super isEqual:object];
    }
    // 如果self和object代表同一个对象，比如这两个对象内存地址不同，但是所有的属性值都相等，对于 SODownloader 来说，就是下载地址相同时返回 YES，否则返回 NO。
    ResourceItemModel *model = (ResourceItemModel *)object;
    return [self.r_id isEqualToString:model.r_id];
}

/**
 为相等的对象返回相同的hash值，为不相等的对象返回不同的hash值。
 */
- (NSUInteger)hash {
    return [self.r_id hash];
}

@end
